package ngames.com.sushitv.action;

import android.app.Activity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.adapter.SearchRequestAdapter;
import ngames.com.sushitv.adapter.SearchUserAdapter;
import ngames.com.sushitv.base.BaseListAction;
import ngames.com.sushitv.core.ModelCallback;
import ngames.com.sushitv.entity.Anime;
import ngames.com.sushitv.entity.SearchRequestAll;
import ngames.com.sushitv.entity.UpUser;

/**
 * recycleView的重用
 */
public class SearchUserAction extends BaseListAction {
    private String mDataID = "0";
    private RecyclerView mRecycler;
    private SwipeRefreshLayout mSwipeRefresh;
    private SearchUserAdapter mAdapter;
    private int Page = 1;
    private String type;
    private String name;

    public SearchUserAction(Activity activity, View baseView, int Page, String type, String name) {
        super(activity, baseView);
        this.Page = Page;
        this.type = type;
        this.name = name;
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        mRecycler = (RecyclerView) mBaseView.findViewById(R.id.recycler_searchRequest);
        mSwipeRefresh = (SwipeRefreshLayout) mBaseView.findViewById(R.id.swipeRefresh_searchRequest);

        //每个item高度一致，可设置为true，提高性能
        //mRecycler.setHasFixedSize(true);
        //mRecycler.setLayoutManager(mLinearLayoutManager);
        mRecycler.setLayoutManager(new GridLayoutManager(mActivity, 1));

        onRefreshData();

    }

    @Override
    public void onRefreshData() {
        videoRank();
    }

    private void videoRank() {
        getAppModel().UserList(type, name, Page + "", new ModelCallback<ArrayList<UpUser>>() {
            @Override
            public void onBeforeRequest(String urlTag) {

            }

            @Override
            public void onSuccess(ArrayList<UpUser> data) {
                mIsLoadingMore = false;
                //mSwipeRefreshLayout.setRefreshing(false);
                if (mAdapter != null) {
                    mAdapter.clearData();
                }
                if (isEmptyList(data)) {
                    return;
                }
                setListData(data);
            }

            @Override
            public void onFailure(String errorEvent, String message) {
                Log.e("fail", errorEvent + "\n" + message);
            }
        });

    }


    public void setListData(ArrayList<UpUser> data) {

            mAdapter = new SearchUserAdapter(mActivity, data);
            mRecycler.setAdapter(mAdapter);



    }
}
